// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const { show } = require("../../game/alert");
const EventServices = require("../../util/EventServices");
const { shopData, player } = require("../gameData");

cc.Class({
    extends: cc.Component,

    properties: {
        sliderMoney: cc.Slider,
        sliderAds: cc.Slider,
        addMoney: cc.Label,
        addFamous: cc.Label,
        moneyTips: cc.Label,
        turnStock: 0,
        turnMoney:0,
        cost:0,
        famous:0,
        allMoney: player.money,
        sliderTurnMoney: cc.Slider,
    },
    ensureAddMoney() {
        //确定融资
        if(this.turnProcess >= 1){
            show('未来金融','无法转让公司噢？are you kiding me');
            return 
        }
        if(this.turnProcess <= 0 || Math.floor(this.turnStock) <= 1) {
            show('未来金融','别瞎gao');
            return ;
        }
        shop.money += this.turnMoney;
        shop.leftStock -= this.turnStock;
        this.sliderMoney.progress = 0;
        show('未来投资公司', `转让${formatMoney(this.turnStock)}股份，获取融资${formatMoney(this.turnMoney)}`);
        this.turnMoney = 0;
        this.turnProcess = 0;
        this.addMoney.string =  `0% / 0元`;
        this.turnStock = 0;
        
    },
    ensureAddFamous() {
        if(shop.money < this.cost) {
            show('未来金融',"钱攒够再来吧，小子");
            return ;
        }
        if(this.cost == 0) {
            return ;
        }
        shop.money -= this.cost;
        // shop.famous += this.famous;
        // if(shop.famous >= 10000) {
        //     shop.famous = 10000;
        // }
        this.sliderAds.progress = 0;
        this.addFamous.string = 0;
        this.cost = 0;
        show('未来金融',`恭喜，收获${formatMoney(this.famous)}的知名度,尝试运营店铺再来效果更好噢`);
        EventServices.publish('addFamous', this.famous);

    },
    onSliderStock(target, i){
        console.log(target.progress,shop.leftStock);
        this.turnProcess = target.progress;
        this.turnStock = (this.turnProcess * shop.leftStock).toFixed(1);//当前想转让的股份
        //收入/投资金额 回报率 
        this.turnMoney = shop.income * (1 + shop.famous % 25 / 100) * this.turnStock + shop.startMoney * 0.1;
        this.addMoney.string = `${formatMoney(this.turnStock)}% / ${formatMoney(this.turnMoney)}元`;
    },
    onSliderFamous(target, i){
        console.log(target.progress);
        this.cost = target.progress * shop.money;
        this.famous = parseInt(Math.random() * shop.loaction + this.cost/200 + shop.famous/50);
        if(this.famous >= 10000) {
            this.famous = 10000;
        }
        this.addFamous.string = formatMoney( this.cost );
    },
    close() {
        this.node.active = !this.node.active;
    },
    
    onSliderTurnMoney (target, i) {
        this.turnMoneyProcess = target.progress;
        this.turnCount = Math.floor(this.turnMoneyProcess * this.allMoney);
        this.moneyTips.string = formatMoney(this.turnCount);
    },
    changeTurnMode (target, i) {
        this.turnMode = i;
        i == 1? this.allMoney = player.money:  this.allMoney = shop.money;
        this.sliderTurnMoney.progress = 0;
        this.moneyTips.string = `最高${formatMoney(this.allMoney)}`;
    },
    ensureTurnMoney() {
        
        if(!this.turnMode){
            show('未来银行','弄啥类，请选择您要的服务');
            return;
        }
        if(isNaN(this.turnCount)) return ;
        if(this.turnCount <= 0) {
            show('未来银行','弄啥类，提多少钱');
            return ;
        }
        if(this.turnMode == 1) {
            shop.money +=(this.turnCount * 0.9);
            player.money -= (this.turnCount * 0.9);
            this.allMoney = player.money;
            show('未来金融','操作成功，扣除10%的手续费和服务费')
        }else {
            shop.money -= (this.turnCount * 0.9);
            player.money += (this.turnCount * 0.9);
            this.allMoney = shop.money;
            show('未来金融','操作成功，扣除10%的手续费和服务费');

        }
        this.moneyTips.string = `最高${formatMoney(this.allMoney)}`;
        this.turnCount = 0;
        this.sliderTurnMoney.progress = 0;
    },
    // onLoad () {},

    start () {
        shop = shopData.shopList.find(val=>val.id == shopId);
        console.log(shop);
    },

    // update (dt) {},
});
